package fr.emse.tatiana.scoresheetvisualisation;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import fr.emse.tatiana.graphical.DisplayRule;
import fr.emse.tatiana.replayable.IReplayableModel;

public class EditRuleDialog extends Dialog{

	ComboViewer facetviewer;
	ComboViewer facetvalueviewer;
	protected String facetname;
	String facetvalue;
	protected String property;
	String propertyvalue;
	IReplayableModel model;
	String selection3;
	String selection1;
	String  selection2;
	String  selection4;
	
	protected EditRuleDialog(Shell parentShell,IReplayableModel model,String selection1,String selection2,String  selection3,String  selection4) {
		super(parentShell);
		this.model=model;
		this.selection1=selection1;
		this.selection2=selection2;
		this.selection3=selection3;
		this.selection4=selection4;
		// TODO Auto-generated constructor stub
	}
	

	protected Control createDialogArea(Composite parent) {
	    Composite comp = (Composite) super.createDialogArea(parent);
	    Composite container = new Composite(comp, SWT.NONE);
        container.setLayout(new GridLayout());
        GridData griddata = new GridData(GridData.FILL, GridData.FILL, true,
				true);
        griddata.verticalSpan = 1;
        griddata.horizontalSpan = 1;
        griddata.widthHint=500;
        container.setLayoutData(griddata);
        
        new Label(container, SWT.NONE).setText("Facet");
        
    	facetviewer = new ComboViewer(container);
		facetviewer.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				if (inputElement == null) {
					return new Object[] {};
				}
				IReplayableModel model = (IReplayableModel) inputElement;
				List<String> ret = new Vector<String>();
				ret.add("Any");
				for (int i = 0; i < model.getFacetCount(); i++) {
					ret.add(model.getFacetName(i));
				}
				return ret.toArray();
			}

			public void dispose() {
			}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
			}

		});

		facetviewer
		.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				String elem = (String) ((IStructuredSelection) event
						.getSelection()).getFirstElement();
				if ("Any".equals(elem)) {
					facetname = null;
				} else {
					facetname = elem;
				}
				facetvalueviewer.refresh();
			}
		});

		facetviewer.setInput(model);
		
		new Label(container, SWT.NONE).setText("FacetValue");
		
		facetvalueviewer = new ComboViewer(container);

		facetvalueviewer.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				if (inputElement == null) {
					return new Object[] {};
				}
				IReplayableModel model = (IReplayableModel) inputElement;
				Set<String> ret = new HashSet<String>();
				for (int e = 0; e < model.getEventCount(); e++) {
					Object event = model.getEvent(e);
					if (facetname == null) {
						for (int i = 0; i < model.getFacetCount(); i++) {
							String s = model.getFacetText(event, i);
							ret.add((s != null && s.length() > 50) ? s
									.substring(0, 50)
									+ "..." : s);
						} // TODO don't do this here, do it in a label provider
					} else {
						String s = model.getFacetText(event, model.getFacetCol(facetname));
						ret.add((s != null && s.length() > 50) ? s.substring(0,
								50)
								+ "..." : s);
					}
				}
				List<String> l = new LinkedList<String>(ret);
				if (l.contains(null)) {
					l.remove(null);
				}
				java.util.Collections.sort(l);
				l.add(0, "Any");
				return l.toArray();
			}

			public void dispose() {
			}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
			}

		});

		facetvalueviewer
		.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				String elem = (String) ((IStructuredSelection) event
						.getSelection()).getFirstElement();
				if ("Any".equals(elem)) {
					facetvalue = null;
				} else {
					facetvalue = elem;
				}
			}
		});

		facetvalueviewer.setInput(model);
		
		
		new Label(container, SWT.NONE).setText("Property");
		
		final ComboViewer propertyviewer = new ComboViewer(container);
		propertyviewer.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				return DisplayRule.properties;
			}

			public void dispose() {
			}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
			}

		});

		propertyviewer
		.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				String elem = (String) ((IStructuredSelection) event
						.getSelection()).getFirstElement();
				property = elem;
			}
		});

		propertyviewer.setInput(new Object());
		

		new Label(container, SWT.NONE).setText("PropertyValue");
		final Text propertyvaluew = new Text(container, SWT.BORDER);

		propertyvaluew.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				propertyvalue = ((Text) e.getSource()).getText();

			}
		});
		/*
		StructuredSelection selec1= new StructuredSelection (selection1);
    	facetviewer.setSelection (selec1);
    	StructuredSelection selec2 = new StructuredSelection (selection2);
    	facetvalueviewer.setSelection (selec2);
    	StructuredSelection selec3 = new StructuredSelection (selection3);
    	propertyviewer.setSelection (selec3);
    	propertyvaluew.setText(selection4);   
    	*/
    	
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			 
            public void run() {
            	StructuredSelection selec1= new StructuredSelection (selection1);
            	facetviewer.setSelection (selec1);
            	StructuredSelection selec2 = new StructuredSelection (selection2);
            	facetvalueviewer.setSelection (selec2);
            	StructuredSelection selec3 = new StructuredSelection (selection3);
            	propertyviewer.setSelection (selec3);
            	propertyvaluew.setText(selection4);      	
            	
            }

        });
		
		return comp;
	}

}
